package com.itheima.utils;


import com.itheima.config.MyJwtProperties;
import io.jsonwebtoken.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class MyJwtUtils {
    @Autowired
    private MyJwtProperties properties;

    public String createJwt(Map<String,Object> map){
        String s = Jwts.builder()
                .setClaims(map)
                .signWith(SignatureAlgorithm.valueOf(properties.getSiginType()),properties.getKey())
                .setIssuedAt(new Date(System.currentTimeMillis()+properties.getTime()))
                .compact();
        return s;
    }


    public Map<String,Object> parseJwt(String jwt){
        Claims body = null;
        try {
            body = Jwts.parser()
                    .setSigningKey(properties.getKey())
                    .parseClaimsJws(jwt)
                    .getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  body;
    }


}
